﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Portal.Logic;
using OC.Common.Entity;
using Portal.Common.Entity;
using System.Collections;
using System.Text;

namespace Portal
{
    public partial class PersonSettingMenu : System.Web.UI.Page
    {

        public string _currentMenu = "";
        public string _currentMenuCategory = "";
        List<DectionaryEntity> parentLists;//父节点
        List<DectionaryEntity> subItems;//子节点
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request["menuName"]))
                {

                    _currentMenu = Request["menuName"];
                }
                if (!string.IsNullOrEmpty(Request["menucategory"]))
                {

                    _currentMenuCategory = Request["menucategory"];
                }
            }
        }

        public void BindTreeView()
        {
            UserEntity list = Session["UserInfo"] as UserEntity;
            if (list != null)
            {
                List<PersonSettingEntity> settinglist = new PersonSettingLogic().GetListByUserId(list.USER_ID);
                TreeNode node = new TreeNode();
                if (settinglist != null)
                {
                    node.Text = "我的收藏";
                    node.ImageUrl = "";
                    foreach (PersonSettingEntity entity in settinglist)
                    {
                        TreeNode subNode = new TreeNode();
                        subNode.Text = entity.SETTING_ITEMS;
                        subNode.NavigateUrl = "javascript:GetSelectedNodeValue('" + entity.SETTING_VALUE + "');";
                        node.ChildNodes.Add(subNode);
                        //tvSettingMenuInfo.Nodes.Add(node);
                    }

                }
                subItems = new List<DectionaryEntity>();
                if (parentLists != null)
                {
                    foreach (DectionaryEntity entity in parentLists)
                    {
                        node = new TreeNode();
                        node.Text = entity.CN_NAME;
                        node.NavigateUrl = entity.EXT01;

                        subItems = new DectionaryLogic().GetSubList(entity.DICTIONARY_ID);
                        foreach (DectionaryEntity subEntity in subItems)
                        {
                            TreeNode subNode = new TreeNode();
                            subNode.Text = subEntity.CN_NAME;
                            subNode.NavigateUrl = "javascript:GetSelectedNodeValue('" + subEntity.EXT01 + "');";
                            node.ChildNodes.Add(subNode);
                        }
                        //tvSettingMenuInfo.Nodes.Add(node);
                    }
                }
            }
        }
        #region 获取所有父节点
        /// <summary>
        /// 获取所有父节点
        /// </summary>
        /// <returns></returns>
        public List<DectionaryEntity> GetParentLists()
        {
            IList lists = Session["UserDeictionaryList"] as IList;
            List<DectionaryEntity> objSet = null;
            if (lists != null)
            {
                if (lists.Count > 0)
                {
                    
                    objSet = (List<DectionaryEntity>)lists;
                    parentLists = objSet.FindAll(delegate(DectionaryEntity ety)
                    {
                        if (ety.PARENT_DICTIONARY_ID.ToString().Trim() == "0" && ety.DICTIONARY_TYPE.ToUpper() == "MENU")
                        {
                            return true;
                        }
                        return false;
                    });

                }

            }
            return parentLists;
        }
        #endregion
        #region 获取所有子节点
        /// <summary>
        /// 获取所有子节点
        /// </summary>
        /// <returns></returns>
        public List<DectionaryEntity> GetChildLists(string parentId)
        {
            subItems = new List<DectionaryEntity>();

            subItems = new DectionaryLogic().GetSubList(parentId);

            return subItems;
        }
        #endregion
        #region 获取字符串
        public string MenuString
        {
            get
            {
                if (ViewState["menuTree"] != null)
                {
                    return ViewState["menuTree"].ToString();
                }
                else
                {
                    string str = BuildMenuTree();
                    return str;
                }
            }
        }
        #endregion
        #region 拼接字符串
        string BuildMenuTree()
        {
            UserEntity list = Session["UserInfo"] as UserEntity;
            StringBuilder sb = new StringBuilder();
            if (list != null)
            {
                //List<PersonSettingEntity> settinglist = new PersonSettingLogic().GetListByUserId(list[0].USER_ID);
                #region  注释文件
                //if (settinglist != null)
                //{
                //    sb.AppendLine("<ul class=\"menulist\"  id='uh" + 1 + "' >");
                //    foreach (PersonSettingEntity entity in settinglist)
                //    {
                //        string childmenuName = entity.SETTING_ITEMS;
                //        string childmenuId = entity.SETTING_ID;
                //        string childmenuValue = entity.SETTING_VALUE;
                //        string ext02 = entity.SETTING_VALUE; //url

                //        if (ext02.IndexOf("?") > 0)
                //        {
                //            ext02 = ext02 + "&menuName=" + Server.UrlEncode(childmenuName) + "&settingValue=" + childmenuValue;
                //        }
                //        else
                //        {
                //            ext02 = ext02 + "?menuName=" + Server.UrlEncode(childmenuName) + "&settingValue=" + childmenuValue;
                //        }
                //        sb.AppendLine("<li ><input type='image' src=\"images/fav.jpg\" value='' onclick=\"PersonSetting('" + childmenuName + "','" + childmenuValue + "')\" /><a href='#' onclick=\"GetSelectedNodeValue('"+ext02+"');\">" + childmenuName + "</a></li>").ToString();
                //        //sb.AppendLine(str);


                //    }
                //    sb.AppendLine("</ul>");
                //}
                #endregion
                List<DectionaryEntity> parentList = GetParentLists();
                if (parentList != null)
                {
                    foreach (DectionaryEntity parentEntity in parentList)
                    {
                        if (parentEntity.DICTIONARY_ID.Trim() == "87")
                        {
                            string parDectionaryId = parentEntity.DICTIONARY_ID.Trim();
                            string menuName = parentEntity.CN_NAME.Trim();
                            //string ext02 = parentEntity.EXT01; //css class
                            //string str = BuildMenu(menuName, "menulist", parDectionaryId);
                            sb.AppendLine("<div title='" + menuName + "'><ul class=\"menulist\"  id='uh" + parDectionaryId + "' >");
                            //sb.AppendLine(str);
                            List<DectionaryEntity> childLists = GetChildLists(parDectionaryId);
                            foreach (DectionaryEntity childEntity in childLists)
                            {

                                string childmenuName = childEntity.CN_NAME.Trim();
                                string childmenuId = childEntity.DICTIONARY_ID.Trim();
                                string ext02 = childEntity.EXT02; //url
                                if (!string.IsNullOrEmpty(ext02))
                                {
                                    if (ext02.IndexOf("?") > 0)
                                    {
                                        ext02 = ext02 + "&menuName=" + Server.UrlEncode(childmenuName) + "&settingValue=" + childmenuId;
                                    }
                                    else
                                    {
                                        ext02 = ext02 + "?menuName=" + Server.UrlEncode(childmenuName) + "&settingValue=" + childmenuId;
                                    }
                                }
                                sb.AppendLine("<li ><input type='image' src=\"images/fav.jpg\" value='' onclick=\"PersonSetting('" + childmenuName + "','" + childmenuId + "')\" /><a href='#' onclick=\"changeSelect(this,'" + ext02 + "');\">" + childmenuName + "</a></li>").ToString();
                            }
                            sb.AppendLine("</ul></div>");
                        }
                        else
                        {
                            string parDectionaryId = parentEntity.DICTIONARY_ID.Trim();
                            string menuName = parentEntity.CN_NAME.Trim();
                            string ext01 = parentEntity.EXT02; //css class
                            //string str = BuildMenu(menuName, "menulist", parDectionaryId);

                            sb.AppendLine("<div title='" + menuName + "'><ul class=\"menulist\"  id='uh" + parDectionaryId + "' >");
                            //sb.AppendLine(str);
                            List<DectionaryEntity> childLists = GetChildLists(parDectionaryId);
                            foreach (DectionaryEntity childEntity in childLists)
                            {

                                string childmenuName = childEntity.CN_NAME.Trim();
                                string childmenuId = childEntity.DICTIONARY_ID.Trim();
                                string ext02 = childEntity.EXT02; //url
                                if (!string.IsNullOrEmpty(ext02))
                                {
                                    if (ext02.IndexOf("?") > 0)
                                    {
                                        ext02 = ext02 + "&menuName=" + Server.UrlEncode(childmenuName) + "&menucategory=" + childmenuId;
                                    }
                                    else
                                    {
                                        ext02 = ext02 + "?menuName=" + Server.UrlEncode(childmenuName) + "&menucategory=" + childmenuId;
                                    }
                                }
                                string str = BuildChildMenu(childmenuName, ext02);
                                sb.AppendLine(str);

                            }
                            sb.AppendLine("</ul></div>");
                        }

                    }

                }
            }
            ViewState["menuTree"] = sb.ToString(); ;
            return sb.ToString();
        }
        #endregion
        #region 拼接父节点字符串
        string BuildMenu(string menuName, string menuClass, string resourceId)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<h3 class=\"" + menuClass + "\" id='h" + resourceId + "' style=\"cursor:hand\" ><strong><a href='#'>" + menuName + "</a></strong></h3>");
            return sb.ToString();
        }
        #endregion
        #region 拼接子节点字符串
        string BuildChildMenu(string menuName, string url)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<li ><a href='#' onclick=\"changeSelect(this,'"+ url + "');\">" + menuName + "</a></li>");
            return sb.ToString();
        }
        #endregion

    }
}